{% extends "base.html" %}

{% block head %}
<style type="text/css" media="all">
  @import "{{ MEDIA_URL }}css/thickbox.css";
</style>
<script>var tb_pathToImage = "{{ MEDIA_URL }}imgs/loadingAnimation.gif";</script>
<script src="{{ MEDIA_URL }}js/thickbox.js" type="text/javascript"></script>
{% endblock %}

{% block content %}
<h1>Full Form View</h1>
<p>No more words than the code below... explicit enough to begin with.</p>
Click <a class="thickbox" href="/form/all/form/?height=300&width=300">this link to try it live</a>.
<div class="highlight"><pre>
<span class="kn">from</span> <span class="nn">obviews.views</span> <span class="kn">import</span> <span class="n">View</span><span class="p">,</span> <span class="n">FormView</span><span class="p">,</span> <span class="n">AjaxFormView</span><span class="p">,</span> <span class="n">CutResponse</span>

<span class="k">class</span> <span class="nc">AllFormView</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="c"># this page</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;forms/all.html&quot;</span>
    
<span class="k">class</span> <span class="nc">ColorForm</span><span class="p">(</span><span class="n">BaseForm</span><span class="p">):</span>
    <span class="c"># the form class</span>
    <span class="n">integer</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">IntegerField</span><span class="p">()</span>

<span class="k">class</span> <span class="nc">ResultsView</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="c"># page when JS not activated</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;forms/results.html&quot;</span>

    <span class="k">def</span> <span class="nf">build_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&quot;integer&quot;</span><span class="p">:</span> <span class="nb">int</span><span class="p">(</span><span class="n">integer</span><span class="p">),</span>
                <span class="s">&quot;color&quot;</span><span class="p">:</span> <span class="s">&quot;#</span><span class="si">%06x</span><span class="s">&quot;</span><span class="o">%</span> <span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">integer</span><span class="p">)</span><span class="o">%</span><span class="mi">16</span><span class="o">**</span><span class="mi">6</span><span class="p">)}</span>
    
<span class="k">class</span> <span class="nc">AllForm</span><span class="p">(</span><span class="n">AjaxFormView</span><span class="p">):</span>
    <span class="c"># the form view (with or without JS)</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;forms/all_form.html&quot;</span>
    <span class="n">form_class</span> <span class="o">=</span> <span class="n">ColorForm</span>

    <span class="k">def</span> <span class="nf">process_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">variables</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&quot;integer&quot;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">ajax_success</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">built_kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">&quot;#</span><span class="si">%06x</span><span class="s">&quot;</span><span class="o">%</span> <span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">result</span><span class="p">)</span><span class="o">%</span><span class="mi">16</span><span class="o">**</span><span class="mi">6</span><span class="p">))</span>
    
    <span class="k">def</span> <span class="nf">noajax_success</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">built_kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="s">&quot;/form/all/results/</span><span class="si">%s</span><span class="s">/&quot;</span><span class="o">%</span> <span class="n">result</span><span class="p">)</span>

<span class="n">urlpatterns</span> <span class="o">+=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>                
    <span class="c"># all options of forms</span>
    <span class="p">(</span><span class="s">&#39;^form/all/$&#39;</span><span class="p">,</span> <span class="n">AllFormView</span><span class="p">()),</span>
    <span class="p">(</span><span class="s">&#39;^form/all/form/$&#39;</span><span class="p">,</span> <span class="n">AllForm</span><span class="p">()),</span>
    <span class="p">(</span><span class="s">&#39;^form/all/results/(?P&lt;integer&gt;.*)/$&#39;</span><span class="p">,</span> <span class="n">ResultsView</span><span class="p">()),</span>
<span class="p">)</span>
</pre></div>
{% endblock %}
